﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 软构大作业
{
    public partial class Form4 : Form
    {
        private new AutoAdaptWindowsSize AutoSize;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        public Form4()
        {
            InitializeComponent();
            checkBox2.CheckedChanged += checkBox2_CheckedChanged;
            checkBox1.CheckedChanged += checkBox1_CheckedChanged;
            // 设置窗体的KeyPreview属性为true，以便窗体可以接收键盘事件
            this.KeyPreview = true;
            // 为窗体添加KeyDown事件处理
            this.KeyDown += Form4_KeyDown;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Form4_FormClosed(object sender, FormClosedEventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            Global.Instance.form4login=null;
            Global.Instance.form3login.Show();
        }

        private void Form4_KeyDown(object sender, KeyEventArgs e)
        {
            // 检查是否按下Enter键
            if (e.KeyCode == Keys.Enter)
            {
                // 触发button1的点击事件
                button1.PerformClick();
                // 标记事件已处理，防止其他控件处理该事件
                e.Handled = true;
                e.SuppressKeyPress = true; // 防止发出"叮"的声音
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string newPassword = textBox1.Text.Trim();
            string confirmPassword = textBox2.Text.Trim();

            // 1. 检查新密码是否为空
            if (string.IsNullOrEmpty(newPassword))
            {
                MessageBox.Show("请输入新密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Focus();
                return;
            }

            // 2. 检查确认密码是否为空
            if (string.IsNullOrEmpty(confirmPassword))
            {
                MessageBox.Show("请确认新密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox2.Focus();
                return;
            }

            // 3. 检查两次输入的密码是否一致
            if (newPassword != confirmPassword)
            {
                MessageBox.Show("两次输入的密码不一致，请重新输入", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox1.Text = "";
                textBox2.Text = "";
                textBox1.Focus();
                return;
            }

            // 4. 检查当前用户是否有效
            if (Global.Instance.CurrentUser == null || string.IsNullOrEmpty(Global.Instance.CurrentUser.Username))
            {
                MessageBox.Show("用户会话无效，请重新登录", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Hide();
                Global.Instance.form1login.Show();
                return;
            }

            // 5. 更新数据库中的密码
            try
            {
                string sql = "UPDATE library_db.users SET password = @Password WHERE username = @Username";
                SqlParameter[] parameters = new SqlParameter[]
                {
            new SqlParameter("@Password", newPassword),
            new SqlParameter("@Username", Global.Instance.CurrentUser.Username)
                };

                // 使用之前定义的 Data.ExecuteQuery 方法（或改用 ExecuteNonQuery）
                int rowsAffected = Data.ExecuteNonQuery(sql, parameters);

                if (rowsAffected > 0)
                {
                    MessageBox.Show("密码修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textBox1.Text = "";
                    textBox2.Text = "";
                    Global.Instance.form4login.Hide();
                    Global.Instance.form1login.Show();
                }
                else
                {
                    MessageBox.Show("密码修改失败，请重试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"数据库错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            textBox1.UseSystemPasswordChar = !checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            textBox2.UseSystemPasswordChar = !checkBox2.Checked;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            Global.Instance.form4login.Hide();
            Global.Instance.form3login.Show();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            AutoSize = new AutoAdaptWindowsSize(this);
            this.SizeChanged += new EventHandler(Form_SizeChanged);
        }

        private void Form_SizeChanged(object sender, EventArgs e)
        {//窗体大小改变事件
            if (AutoSize != null) // 一定加这个判断，电脑缩放布局不是100%的时候，会报错
            {
                AutoSize.FormSizeChanged();
            }
        }
    }
}
